<template>
  <el-dialog
    :title="'新增'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称">
      </el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goodsNum">
      <el-input type="number" :step="1" min="1" v-model="dataForm.goodsNum" placeholder="商品数量">
        <template slot="append">件</template>
      </el-input>
    </el-form-item>
    <el-form-item label="商品规格" prop="goodsSpecs">
      <el-input v-model="dataForm.goodsSpecs" placeholder="商品规格">
        <template slot="append">kg/件</template>
      </el-input>
    </el-form-item>
    <el-form-item label="商品种类" prop="goodsType">
      <el-select v-model="dataForm.goodsType" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="库位" prop="storageSpaceId">
      <el-select v-model="dataForm.storageSpaceId" placeholder="请选择">
        <el-option v-for="item in spaceOptions"
                     :key="item.id"
                     :label="item.spaceName"
                     :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        options: [],
        spaceOptions: [],
        visible: false,
        dataForm: {
          id: 0,
          goodsName: '',
          goodsNum: '',
          contractId: '',
          goodsType: '',
          goodsSpecs: '',
          storageSpaceId: ''
        },
        dataRule: {
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsNum: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          goodsType: [
            { required: true, message: '商品种类不能为空', trigger: 'blur' }
          ],
          goodsSpecs: [
            { required: true, message: '商品规格不能为空', trigger: 'blur' }
          ],
          storageSpaceId: [
            { required: true, message: '库位不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, storageId) {
        this.dataForm.contractId = id
        this.visible = true
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/commodity_type`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/app/entstoragespace/queryStorageSpace/${storageId}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.spaceOptions = data.list
          }
        })
      },
      
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entstocontractgoods/save`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsName': this.dataForm.goodsName,
                'goodsNum': this.dataForm.goodsNum,
                'contractId': this.dataForm.contractId,
                'goodsType': this.dataForm.goodsType,
                'goodsSpecs': this.dataForm.goodsSpecs,
                'storageSpaceId': this.dataForm.storageSpaceId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
